/*--------------------------------------------------------------------------------
	Hujub Exporter	Matrix Exp
				by Hujub SungiL
	
	HISTORY	: 2005_10_16

    LAST UPDATE : 2005_10_16
--------------------------------------------------------------------------------*/
#pragma once


#include "AnimStructure.h"
#include "MaterialMng.h"
#include ".\\util\\HjUtil.h"
#include "BipedMng.h"


class CMatrixExp
{
public:
	Interface*			m_pInterface;
	IBipedExport*		m_pIBipExp;
	INode*				m_pLootNode;

	int					m_nChildNode;
	int					m_nGeom;
	int					m_nTotNodeCnt;
	int					m_nBipIndex;
	int					m_nStaticFrame;
	int					m_nKeyFrameStep;
	int					m_nCurNode;
	Interval			m_Interval;

	FILE*				m_fpBipInherit;
	FILE*				m_fpBipTri;
	FILE*				m_fpBipList;
	FILE*				m_pFile;

	Scene				m_Scene;
	Ani_Geom*			m_pGeom;
	CMaterialMng		m_pMtrlMng;

	float*				m_pfNegative;
	Quat**				m_ppScaleAxis;
	Quat**				m_ppRot;
	Point3**			m_ppPos;
	Point3**			m_ppScaleVal;
	Matrix3**			m_ppAnimTM;

	D3DXMATRIX*			m_pInverseTM;

	BipMesh*			m_pBipMesh;
	int*				m_npParent;
	D3DXMATRIX**		m_ppAnimation;

	int					m_nMaxBiped;

public:
	VOID	Init();
	VOID	PreProcess( INode* pNode , int& nNodeCnt , int nLevel ,int& nBipIndex );
	BOOL	InitMatExp( Interface* pInterface );
	BOOL	Tick();
	BOOL	Release();

    VOID	SetScene();
	VOID	SetGeomObject();
	BOOL	LoadAnimationMatrix();
	VOID	SetInitMatrix( int nNumFrame );

	VOID	WriteGeom( INode* pNode );
	VOID	WriteNodeHeader( INode* pNode , Ani_Geom* pGeom );
	VOID	WriteNodeTM( INode* pNode , Ani_Geom* pGeom );
	VOID	WriteMesh( INode* pNode , TimeValue t , Ani_Geom* pGeom );
	VOID	WriteAnimKeys( INode* pNode , Ani_Geom* pGeom );
	VOID	WritePosSample( INode* pNode , Ani_Geom* pGeom );
	VOID	WriteRotSample( INode* pNode , Ani_Geom* pGeom );
	VOID	WriteScaleSample( INode* pNode , Ani_Geom* pGeom );
	VOID	WriteHelperObj( INode* pNode );
	BOOL	WriteForFile( TCHAR* szAniMatPath );

	BOOL	NodeEnum( INode* pNode );
	TSTR	GetIndent( int nIndentLevel );
	TriObject*	GetTriObjectFromNode( INode* pNode , TimeValue t , BOOL& deleteIt );
	VOID	DumpMatrix3( Matrix3* m , D3DXMATRIX& mat );
	BOOL	CheckAnimaton( INode* pNode , BOOL& bPos , BOOL& bRot , BOOL& bScale );
	BOOL	EqualPoint3( Point3 p1 , Point3 p2 );
	AffineParts	GetAffineParts( INode* pNode , TimeValue tv );
	BOOL	GetNodeID( TCHAR* szpName , int& nNodeID );
	VOID	SetBuffer( Ani_Geom* pGeom , int nNode );

    Ani_Geom*	DeleteGeom( Ani_Geom* pGeom );
	AnimLink*	DelAnimTrack( AnimLink*	pAnim );


	// Inline Func
	inline	BOOL	TMNegParity( Matrix3 &m ) {
		return ( DotProd( CrossProd( m.GetRow(0) , m.GetRow(1)) , m.GetRow(2)) < 0.0 ) ? 1 : 0;
	}

    
	inline	Interface*	GetInterface()	{ return m_pInterface;	};
	inline	INode*		GetLootnode()	{ return m_pLootNode;	};
	inline	FILE*		GetFileStream() { return m_pFile;		};


public:
	CMatrixExp(void) {};
	virtual	~CMatrixExp(void) {};
};
